﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HA.MT3DMS.FileLayer
{
   public class PMFiles
   {

       #region 构造函数
        
       public PMFiles(int numSpecis,List<PmStressPeriod> spConfig)
       {
           this._concFiles = new List<ConcFile>();

           for (int i = 0; i < numSpecis; i++)//污染物的个数 对应要生成的  MT3D001文件夹  等的个数
           {
               //每个 MT3D001文件夹 中生成的浓度文件的控制信息  其生成的文件个数决定于 应力期*时间不长*传输补偿
               string ucnFileName = "mt3d001.ucn";
              
               if (i<10)
               {
                   ucnFileName = "MT3D" + "00" + (i+1).ToString() + ".ucn";
               }
               else if(i>=10 && i<=99)
               {
                   ucnFileName = "MT3D" + "0" + (i + 1).ToString() + ".ucn";
               }
               else if (i >= 100)
               {
                   ucnFileName = "MT3D" + (i + 1).ToString() + ".ucn";
               }

               //ConcFile conc = new ConcFile(ucnFileName, "mt3d.cnf", "");
               ConcFile conc = new ConcFile(ucnFileName, "mt3d.cnf");


               this._concFiles.Add(conc);
               
           }


           this._stressPeriodSetup = spConfig;


 
       }


       #endregion


       #region 字段属性


       #region 变化参数

        //string _concName;
        //public string ConcName
        //{
        //    get { return _concName; }
        //    set { _concName = value; }
        //}
        //string _configName;
        //public string ConfigName
        //{
        //    get { return _configName; }
        //    set { _configName = value; }
        //}
        //string _outFileName;
        //public string OutFileName
        //{
        //    get { return _outFileName; }
        //    set { _outFileName = value; }
        //}

       #endregion


        #region  固定参数


       List<ConcFile> _concFiles = new List<ConcFile>();

       public List<ConcFile> ConcFiles
       {
           get { return _concFiles; }
       }


       List<PmStressPeriod> _stressPeriodSetup;


        int _readFormat = 2;


        bool _offset = false;

        int _specifyIJK = 0;



        int _numI;

        public int NumI
        {
            get { return _numI; }
            set { _numI = value; }
        }
        int _numJ;

        public int NumJ
        {
            get { return _numJ; }
            set { _numJ = value; }
        }

        int _numK;

        public int NumK
        {
            get { return _numK; }
            set { _numK = value; }
        }





        int _outFormat;

        public int OutFormat
        {
            get { return _outFormat; }
            set { _outFormat = value; }
        }







        #endregion



       #endregion


        #region 内部方法

       /// <summary>
        /// 创建文件夹 如 ：pm1  如果存在就删除内容，并新建；不存在则创建
       /// </summary>
       /// <param name="directoryPath"></param>
        private void CreatNewFileDirectory(string directoryPath)
        {


            try
            {

                if (System.IO.Directory.Exists(directoryPath))
                {

                    System.IO.Directory.Delete(directoryPath ,true );
                }

                System.IO.Directory.CreateDirectory(directoryPath );

            }
            catch 
            {

                System.Windows.Forms.MessageBox.Show("创建文件夹的过程出错！");
                return;
            }

        
        }



        private void WriteToFile()
        { 
        
        
        }



        #endregion


        #region 公有方法

       /// <summary>
        /// 传入的路径为文件夹位置的绝对路径 例如：C:\Documents and Settings\Administrator\桌面\mt3d\solutions
        ///  将在该文件夹下创建文件夹存放 pm 的配置文件和 pm.exe 以及生成的浓度文件
       /// </summary>
       /// <param name="path"></param>
        public void WriteFiles(string path)
        {
            //传入的路径为文件夹位置的绝对路径 例如：C:\Documents and Settings\Administrator\桌面\mt3d\solutions

            //每个污染物生成的MT3D001.ucn文件为二进制文件  要使用pm.exe读取  则要配置对应的 pm1.ini 文件  每个 pm1.ini 运行完成后生成大量（应力期*时间不长*传输补偿）浓度文件
            //foreach (ConcFile item in this._concFiles)
            //{
            //    item.WriteFile(path);
            //}

            #region 首先进行一些简单的数据有效性判断

            if (this._concFiles == null || this._stressPeriodSetup == null)
            {
                return;
            }


            #endregion


            System.IO.FileStream fs=null ;
            System.IO.StreamWriter sw=null ;

            try
            {

                #region



                for (int index = 0; index < this._concFiles.Count; index++)
                {


                    string concName = this._concFiles[index].ConcName;// 例如： mt3d001.ucn  mt3d002.ucn   mt3d003.ucn 等

                    string configName = this._concFiles[index].ConfigName;//始终是：mt3d.cnf

                    List<string> outputFileName = this._concFiles[index].OutFileNameList;//存放动态生成的文件名    引用传递

                    if (outputFileName == null)
                    {
                        outputFileName = new List<string>();
                    }
                    else
                    {
                        outputFileName.Clear();

                    }

                    #region 创建并写入对应的 pm.ini 配置文件参数信息  ，并记录动态生成的输出浓度文件名称信息


                    //首先是文件夹的信息

                    string directoryPath = path + "pm" + (index + 1).ToString();

                    this.CreatNewFileDirectory(directoryPath);

                    string filePath = directoryPath + "\\" + "pm" + (index + 1).ToString() + ".ini";

                    fs = new System.IO.FileStream(filePath, System.IO.FileMode.CreateNew);

                    sw = new System.IO.StreamWriter(fs);


                    if (this._stressPeriodSetup.Count > 0)
                    {

                        #region

                        //首先写入一些固定的文件头信息
                        string line1 = concName + "         " + "          */unformatted file name" + "\r\n";
                        string line2 = this._readFormat.ToString() + "                                  */1: LF95-style unformatted; 2: true binary" + "\r\n";
                        string line3 = configName + "                         */configuration file name" + "\r\n";
                        string line4 = "";
                        if (this._offset == false)
                        {
                            line4 = "n                                  */shift offset?" + "\r\n";
                        }
                        else
                        {
                            line4 = "y                                  */shift offset?" + "\r\n";
                        }

                        string line5 = "!!!!0 " + "\r\n";
                        string line6 = "!!!!0" + "\r\n";
                        string line7 = "!!!!!!0                              */x, y,z offsets" + "\r\n";


                        //首先写入前7行
                        sw.WriteLine(line1);
                        sw.WriteLine(line2);
                        sw.WriteLine(line3);

                        sw.WriteLine(line4);
                        sw.WriteLine(line5);
                        sw.WriteLine(line6);

                        sw.WriteLine(line7);


                        //生成的浓度文件的命名规则按照  ： pm1_应力期_时间步长_传输步长.DAT 等等
                        string outFileName = "pm" + (index + 1).ToString();

                        #region 伪代码  添加应力期参数等

                        //if (this._stressPeriodSetup.Count <= 0)
                        //{
                        //    List<int> a = new List<int>();
                        //    a.Add(5);
                        //    a.Add(6);


                        //    List<int> bT = new List<int>();
                        //    bT.Add(1);
                        //    bT.Add(2);

                        //    PmStressPeriod strss = new PmStressPeriod(a, bT);

                        //    this._stressPeriodSetup.Add(strss);
                        //}

                        #endregion



                        #region

                        for (int i = 0; i < this._stressPeriodSetup.Count; i++)
                        {
                            for (int j = 0; j < this._stressPeriodSetup[i].NumTimeStep.Count; j++)
                            {
                                for (int k = 0; k < this._stressPeriodSetup[i].NumTransStep.Count; k++)
                                {
                                    outFileName = "pm" + (index + 1).ToString();

                                    outFileName = outFileName + "_" + (i + 1).ToString() + "_" + this._stressPeriodSetup[i].NumTimeStep[j].ToString() + "_" + this._stressPeriodSetup[i].NumTransStep[k].ToString() + ".DAT";

                                    outputFileName.Add(outFileName);


                                    #region 写入文件

                                    if (i == 0 && j == 0 && k == 0)
                                    {
                                    }
                                    else
                                    {
                                        //添加两行y

                                        sw.WriteLine("y" + "\r\n");
                                        sw.WriteLine("y" + "\r\n");

                                    }

                                    string line8 = "0                                 */total elapsed time" + "\r\n";
                                    string line9 = this._stressPeriodSetup[i].NumTransStep[k].ToString() + "   " + this._stressPeriodSetup[i].NumTimeStep[j].ToString() + "   " + (i + 1).ToString() + "          " + "*/trans step,time step,stress period" + "\r\n";
                                    string line10 = "1" + "\r\n";
                                    string line11 = "1" + "\r\n";
                                    string line12 = "1                          */jjj1,iii1,kkk1" + "\r\n";
                                    string line13 = this.NumJ.ToString() + "\r\n";
                                    string line14 = this.NumI.ToString() + "\r\n";
                                    string line15 = this.NumK.ToString() + "                          */jjj2,iii2,kkk2" + "\r\n";
                                    string line16 = outFileName + "          */output name" + "\r\n";
                                    string line17 = this._outFormat.ToString() + "                  */output format: 1=topo grd; 2=x,y,z,v; 3=Tecplot" + "\r\n";
                                  
                                    
                                    string line18 = "!!!!!!!!!!!!!!!!n                                  */another file?       ......" + "\r\n";

                              

                                    sw.WriteLine(line8);
                                    sw.WriteLine(line9);
                                    sw.WriteLine(line10);
                                    sw.WriteLine(line11);
                                    sw.WriteLine(line12);

                                    sw.WriteLine(line13);
                                    sw.WriteLine(line14);
                                    sw.WriteLine(line15);
                                    sw.WriteLine(line16);
                                    sw.WriteLine(line17);

                                    sw.WriteLine(line18);


                                    //再添加两个空行

                                    sw.WriteLine("\r\n");
                                    sw.WriteLine("\r\n");


                                    #endregion


                                }
                            }

                        }

                        #endregion


                        #endregion


                    }

                    sw.Close();
                    fs.Close();

                    #endregion



                }// for (int index = 0; index < this._concFiles.Count; index++)  结束


                #endregion


            }
            catch
            {
                if (sw!=null )
                {
                    sw.Close();
                }
                if (fs!=null )
                {
                    fs.Close();
                }
                System.Windows.Forms.MessageBox.Show("生成 pm.ini 配置文件过程出错！");
                return;

            }



        }


        #endregion


   }


    /// <summary>
    /// 应力期信息
    /// </summary>
   public class PmStressPeriod
   {

       //public PmStressPeriod(int ntimeStep, int ntransStep)
       //{
          
 
       //}



       public PmStressPeriod(List<int> ntimeStep, List<int> ntransStep)
       {
           this._numTimeStep = ntimeStep;

           this._numTransStep = ntransStep;
 
       }



       List<int> _numTransStep;
       /// <summary>
       /// 传输步长
       /// </summary>
       public List<int> NumTransStep
       {
           get { return _numTransStep; }
           set { _numTransStep = value; }
       }



       List<int> _numTimeStep;
       /// <summary>
       /// 时间步长
       /// </summary>
       public List<int> NumTimeStep
       {
           get { return _numTimeStep; }
           set { _numTimeStep = value; }
       }


      
   }


}
